package com.xhermit.learn.footprint.service.impl;

import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.xhermit.learn.footprint.base.service.BaseServiceImpl;
import com.xhermit.learn.footprint.domain.Log;
import com.xhermit.learn.footprint.mapper.LogMapper;
import com.xhermit.learn.footprint.service.LogService;
/*
 * Spring AOP不能拦截从对象内部调用的方法 
 * http://www.iteye.com/topic/494124
 *  http://blog.csdn.net/z69183787/article/details/45622821
 */
@Service("logService") 
public class LogServiceImpl  extends BaseServiceImpl<LogMapper, Log> implements LogService{
	@Resource
    private PlatformTransactionManager transactionManager  ; 
	
	@Resource(name="logServiceB")
	private LogService  logServiceB;
 
	 @Transactional(propagation=Propagation.REQUIRED)
	public void testTrans(Log log) {
//		TransactionDefinition definition=new DefaultTransactionDefinition();
//		TransactionStatus status=transactionManager.getTransaction(definition);
//	 	try{
		//( (LogService)AopContext.currentProxy()).doBusiness(log); // cglib
		 this.doBusiness(log);
		 logServiceB.doBusiness(log);
		   
//		   transactionManager.commit(status);
//		}catch(RuntimeException e){
//			transactionManager.rollback(status);
//			throw e;
//		}
		
		//log.setUuid(UUID.randomUUID().toString().replaceAll("-", ""));
		//log.setRemark("testTrans commit after error!");
		// dao.insert(log);
		System.out.println("commit Step end! uuid ="+log.getUuid());
		
		// throw new RuntimeException("test Error!");
	}
	/**
	 * 
	 * 调用方法 
	 * @author  FangJun
	 * @date 2016年11月5日
	 * @param log
	 */
	 @Transactional(propagation=Propagation.NEVER)
	public void doBusiness(Log log) {
		log.preInsert();
	    dao.insert(log);
	    
	 // throw new RuntimeException("test 2222 Error!");
	}
	
}